package com.caipin.service.impl;

/**
 * @Author: zhunian0322@163.com
 * @DateTime: 2021-10-06 12:13
 * @Description: TODO
 */
@Service
public class NVRManagementServiceImpl implements NVRManagementService {
    private static final Logger log = LoggerFactory.getLogger(com.caipin.service.impl.NVRManagementServiceImpl.class);

    @Autowired
    private NVRComparisonImagesDao nvrComparisonImagesDao;

    @Autowired
    private NVRComparisonResultsDao nvrComparisonResultsDao;

    @Autowired
    private FaceImageDataDao faceImageDataDao;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private ObjectMapper objectMapper;

    @Value("${faceMatch.portServe}")
    private String url;

    @Transactional
    public void save(FaceMatchEntity faceMatchEntity) {
        Student student = (Student)this.studentDao.selectById(faceMatchEntity.getPersonId());
        NVRComparisonImages images = new NVRComparisonImages();
        images.setBase64(faceMatchEntity.getImageBase64());
        this.nvrComparisonImagesDao.insert(images);
        NVRComparisonResults results = new NVRComparisonResults();
        results.setPersonId(faceMatchEntity.getPersonId())
                .setPersonName(student.getName())
                .setImageId(images.getId())
                .setImageBase64(images.getBase64())
                .setTimestamp(faceMatchEntity.getTimestamp())
                .setCreateTime(new Date());
        try {
            String data = this.objectMapper.writeValueAsString(results);
            String result = HttpClientUtils.doPost(this.url, data, 3000, new HashMap<>());
            if (result == null) {
                log.error("update faceMatch lose");
                results.setUpStatus(Integer.valueOf(0));
            } else {
                results.setUpStatus(Integer.valueOf(1));
                log.info("update success");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        this.nvrComparisonResultsDao.insert(results);
    }
}

